package org.pisces.cas;

import java.util.ArrayList;
import java.util.List;

import org.apereo.cas.web.flow.AbstractCasWebflowConfigurer;
import org.apereo.cas.web.flow.CasWebflowConstants;
import org.springframework.webflow.definition.registry.FlowDefinitionRegistry;
import org.springframework.webflow.engine.ActionState;
import org.springframework.webflow.engine.Flow;
import org.springframework.webflow.engine.builder.support.FlowBuilderServices;
import org.springframework.webflow.execution.Action;

public class MyValidateWebflowConfigurer extends AbstractCasWebflowConfigurer {

	public MyValidateWebflowConfigurer(FlowBuilderServices flowBuilderServices,
			FlowDefinitionRegistry flowDefinitionRegistry) {
		super(flowBuilderServices, flowDefinitionRegistry);
	}

	@Override
	protected void doInitialize() throws Exception {
		final Flow flow = getLoginFlow();
		if (flow != null) {
			final ActionState state = (ActionState) flow.getState(CasWebflowConstants.TRANSITION_ID_REAL_SUBMIT);
			final List<Action> currentActions = new ArrayList<>();
			state.getActionList().forEach(currentActions::add);
			currentActions.forEach(a -> state.getActionList().remove(a));
			state.getActionList().add(createEvaluateAction("myValidateAction"));
			currentActions.forEach(a -> state.getActionList().add(a));
			
			state.getTransitionSet()
					.add(createTransition("captchaError", CasWebflowConstants.STATE_ID_INIT_LOGIN_FORM));
		}
	}

}